package com.ftg.learn.working;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author kangjx
 * @createTime 2022/9/21
 */
public class LearnRabbitConsumer {

    public static void main(String[] args) throws IOException {
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("127.0.0.1");
        cf.setPort(5673);
        cf.setVirtualHost("dev");
        cf.setUsername("kangjx");
        cf.setPassword("kangjx");
        try (
                Connection conn =cf.newConnection() ;
                Channel c = conn.createChannel();
            ){
            DefaultConsumer consumer = new DefaultConsumer(c) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);
                    //对信息进行操作
                    System.out.println("body:"+new String(body));
//                    String message = new String(body, "UTF-8");
//                    try {
//                        doWork(message);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                }
            };
            c.basicConsume("hello1", true, consumer);
        } catch(IOException | TimeoutException e){
            e.printStackTrace();
        }
    }

    private static void doWork(String task) throws InterruptedException {
        System.out.println("---111");
        for (char ch : task.toCharArray()) {
            if (ch == '.') {
                System.out.println("dowork---->");
                Thread.sleep(1000);
            }
        }
    }

}
